/**
 * authentication.js
 * 鉴权reducer, 用在登录视图与其他需要鉴权信息的视图
 *
 * created by qiuwei
 * version 1.0.0
 * 2016年03月07日
 */

import { CONTROL_STATE } from '../../../constants/control';
import { 
	MODIFY_PASSWORD_REQUEST,
	MODIFY_PASSWORD_SUCCESS,
	MODIFY_PASSWORD_FAILURE,
	RESET_MODIFY_PASSWORD
} from '../actions/changeThePassword';

export const INITIALIZE_STATE = {
	// 提示消息
	message: '',
	//  是否
	actionState: 0,
	// 正在和服务器通讯
	isFetching: false
};

export default function changeThePassword(state = INITIALIZE_STATE, action = {}) {
	let { type, ...others } = action;
	switch(type) {
		case MODIFY_PASSWORD_REQUEST: 
			{
				return Object.assign({},state,{
					isFetching:true,
					actionState:0
				})
			}
		case MODIFY_PASSWORD_SUCCESS: 
			//修改密码成功，修改页面状态
			{
				return Object.assign({},state,{
					isFetching:false,
					actionState:1
				})
			}	
		case MODIFY_PASSWORD_FAILURE: 
			//验证用户信息失败，停止请求验证，获取错误消息
			{
				let { errorMessage } = others.payload;
				return Object.assign({},state,{
					isFetching:false,
					actionState:0,
					message:errorMessage
				})
			}
		case RESET_MODIFY_PASSWORD:
			{
				return INITIALIZE_STATE;
			}
		default: 
			return state;
	}
};